﻿Public Class Misc
    Public Shared Function GeneratePassCode() As String
        Randomize()
        Dim clsRet As New System.Text.StringBuilder
        For i As Integer = 0 To 63
            Dim c As Char = Chr(33 + CInt(Rnd() * (255 - 33)))
            clsRet.Append(c)
        Next
        Return clsRet.ToString
    End Function

    Public Shared Function GenerateUniqueId() As String
        Return String.Format("{0}{1}", System.Guid.NewGuid.ToString, System.Guid.NewGuid).Replace("-", "")
    End Function

    Public Shared Sub HandleException(ByVal owner As System.Windows.Forms.IWin32Window, ByVal exception As Exception)
        Dim sText As String = String.Format("{0}{1}{1}{2}", exception.Message, Environment.NewLine, exception.StackTrace)
        System.Windows.Forms.MessageBox.Show(owner, sText, "Unexpected Exception", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation)
    End Sub
    Public Shared Sub HandleException(ByVal exception As Exception)
        HandleException(Threading.MainGuiForm, exception)
    End Sub

    Public Shared Function ToReadableFileSize(ByVal size As Long) As String
        Dim aSuffix() As String = {"b", "Kb", "Mb", "Gb", "Tb"}
        Dim dSize As Double = size
        For i As Integer = 0 To aSuffix.Count - 1
            If dSize < 1024 Or i = aSuffix.Count - 1 Then
                If dSize = CDbl(CInt(dSize)) Then
                    Return String.Format("{0} {1}", CInt(dSize), aSuffix(i))
                Else
                    Return String.Format("{0:0.0} {1}", dSize, aSuffix(i))
                End If
            Else
                dSize = dSize / 1024
            End If
        Next
        Return "?"
    End Function

End Class
